from flask import Blueprint, request
from libs.readcsv import correlationdata
import json
import numpy as np
from flask_cors import CORS

correlation_bp = Blueprint("correlationapi", __name__)
cors = CORS(correlation_bp)


@correlation_bp.route('/relativity', methods=['GET'])
def get_map_value():
    type = request.args.get('type')
    json_list = []
    if type == 'PUT':
        data = correlationdata('PUT')
        data1 = data.to_numpy()
        matrix = np.insert(data1, 0, data.columns, axis=0)
        rows, cols = matrix.shape
        for i in range(1, rows):
            for j in range(1, cols):
                json_obj = {
                    "xField": matrix[i, 0],
                    "yField": matrix[0, j],
                    "value": matrix[i, j]
                }
                json_list.append(json_obj)
        return json.dumps(json_list)
    if type == 'GET':
        data = correlationdata('GET')
        data1 = data.to_numpy()
        matrix = np.insert(data1, 0, data.columns, axis=0)
        rows, cols = matrix.shape
        for i in range(1, rows):
            for j in range(1, cols):
                json_obj = {
                    "xField": matrix[i, 0],
                    "yField": matrix[0, j],
                    "value": matrix[i, j]
                }
                json_list.append(json_obj)
        return json.dumps(json_list)
    if type == 'DELETE':
        data = correlationdata('DELETE')
        data1 = data.to_numpy()
        matrix = np.insert(data1, 0, data.columns, axis=0)
        rows, cols = matrix.shape
        for i in range(1, rows):
            for j in range(1, cols):
                json_obj = {
                    "xField": matrix[i, 0],
                    "yField": matrix[0, j],
                    "value": matrix[i, j]
                }
                json_list.append(json_obj)
        return json.dumps(json_list)

    else:
        return "Handling logic for other types"
